//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Utils/Base/Core.h"
#include "Utils/Shader/ShaderConfig/ShaderConfigLoader.h"
#include "Utils/Texture/Image/ImageConverter.h"
#include "Utils/UI/UIConfigLoader.h"
#include "Utils/Font/FontConverter.h"
#include "Utils/Texture/Sprite/SpriteConverter.h"
#include "Utils/Sound/SoundConverter.h"

namespace Engine
{
namespace Utils
{

	//
	// Resource Converter
	//

	class ResourceConverter
	{
	// variables
	protected:
		Ptr< UtilsCore >	_core;
		ShaderConfigLoader	_shaderLoader;
		ImageConverter		_imageConverter;
		FontConverter		_fontConverter;
		UIConfigLoader		_uiLoader;
		SpriteConverter		_spriteConverter;
		SoundConverter		_soundConverter;


	// metadata
	public:
		struct MetaResConv
		{
			// ui
			string	uiInput,
					uiOutput;
			bool	uiClearDir;

			// textures
			string	texInput,
					texOutput;
			bool	texClearDir;

			// shader programs
			string	progInput,
					progOutput;
			bool	progClearDir;

			// font
			string	fontInput,
					fontOutput;
			bool	fontClearDir;

			// sprite
			string	sprInput,
					sprOutput;
			bool	sprClearDir;

			// sound
			string	sndInput,
					sndOutput;
			bool	sndClearDir;

			///
			MetaResConv () :
				uiClearDir(false), texClearDir(false), progClearDir(false),
				fontClearDir(false), sprClearDir(false), sndClearDir(false)
			{}
		};

		static CMetaClass &		GetMetaClass ()		{ static CMetaClass metaClass;  return metaClass; }
		
		friend UX_CREATE_META_OBJECT( MetaResConv,	GetMetaClass() );


		static void InitMetaData ()
		{
			CMetaAddIteratorPtr	iter;

			if ( not GetMetaClass().IsInit() )
			{
				MetaResConv obj;

				GetMetaClass().CreateExt( "ResourceConverter", obj );
				iter = GetMetaClass().GetAddIter( obj );

				iter->AddField( obj.progClearDir,	"prog_clear" );
				iter->AddField( obj.progInput,		"prog_in"	 );
				iter->AddField( obj.progOutput,		"prog_out"	 );

				iter->AddField( obj.texClearDir,	"tex_clear"  );
				iter->AddField( obj.texInput,		"tex_in"	 );
				iter->AddField( obj.texOutput,		"tex_out"	 );

				iter->AddField( obj.uiClearDir,		"ui_clear"	 );
				iter->AddField( obj.uiInput,		"ui_in"		 );
				iter->AddField( obj.uiOutput,		"ui_out"	 );

				iter->AddField( obj.fontInput,		"font_in"	 );
				iter->AddField( obj.fontOutput,		"font_out"	 );
				iter->AddField( obj.fontClearDir,	"font_clear" );

				iter->AddField( obj.sprInput,		"spr_in"	 );
				iter->AddField( obj.sprOutput,		"spr_out"	 );
				iter->AddField( obj.sprClearDir,	"spr_clear"  );

				iter->AddField( obj.sndInput,		"snd_in"	 );
				iter->AddField( obj.sndOutput,		"snd_out"	 );
				iter->AddField( obj.sndClearDir,	"snd_clear"	 );
			}
		}


	// methods
	public:
		ResourceConverter (UtilsCore *core) :
			_core( core ),				_shaderLoader( core ),
			_uiLoader( core ),			_imageConverter( core ),
			_fontConverter( core ),		_spriteConverter( core ),
			_soundConverter( core )
		{
			InitMetaData();
		}


		bool LoadConfig (uni_c_string filename)
		{
			static_obj_tree_t	cfg_tree;
			MetaResConv			obj;

			cfg_tree.Push( TConfigObjInfo( GetMetaClass().GetSetIter( obj ), null ) );

			CHECK_ERR( _core->Parse( filename, cfg_tree ) );

			return OnLoad( obj );
		}


		bool OnLoad (const MetaResConv &obj)
		{
			if ( not obj.progInput.Empty() ) {
				CHECK( _shaderLoader.LoadConfig( obj.progInput, obj.progOutput, obj.progClearDir ) );
			}
			if ( not obj.texInput.Empty() ) {
				CHECK( _imageConverter.LoadConfig( obj.texInput, obj.texOutput, obj.texClearDir ) );
			}
			if ( not obj.uiInput.Empty() ) {
				CHECK( _uiLoader.LoadConfig( obj.uiInput, obj.uiOutput, obj.uiClearDir ) );
			}
			if ( not obj.fontInput.Empty() ) {
				CHECK( _fontConverter.LoadConfig( obj.fontInput, obj.fontOutput, obj.fontClearDir ) );
			}
			if ( not obj.sprInput.Empty() ) {
				CHECK( _spriteConverter.LoadConfig( obj.sprInput, obj.sprOutput, obj.sprClearDir ) );
			}
			if ( not obj.sndInput.Empty() ) {
				CHECK( _soundConverter.LoadConfig( obj.sndInput, obj.sndOutput, obj.sndClearDir ) );
			}
			return true;
		}
	};


}	// Utils
}	// Engine
